package com.sgb.kjwl.view.ui.activity.enterprise;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityQuestionDetailBinding;
import com.sgb.kjwl.viewmodel.enterprise.QuestionViewModel;

public class QuestionDetailActivity extends AppActivity {

    private ActivityQuestionDetailBinding mBinding;
    private QuestionViewModel mViewModel;


    @Override
    protected void initView() {
        super.initView();
        mViewModel = new ViewModelProvider(this).get(QuestionViewModel.class);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_question_detail);
        mBinding.setVariable(BR.model,mViewModel);
        mBinding.setLifecycleOwner(this);

        if(!TextUtils.isEmpty(getIntent().getStringExtra("id"))){
            mViewModel.getDetail(getIntent().getStringExtra("id"));
        }

        mViewModel.data.observe(this,data ->{
            if(!TextUtils.isEmpty(data.getStraightMatter())){
                mBinding.mweview.getSettings().setDefaultTextEncodingName("UTF-8");
                mBinding.mweview.getSettings().setJavaScriptEnabled(true);//支持JS
                String js = "<script type=\"text/javascript\">" +
                        "var imgs = document.getElementsByTagName('img');" + // 找到img标签
                        "for(var i = 0; i<imgs.length; i++){" +  // 逐个改变
                        "imgs[i].style.width = '100%';" +  // 宽度改为100%
                        "imgs[i].style.height = 'auto';" +
                        "}" +
                        "</script>";
                mBinding.mweview.loadDataWithBaseURL(null, data.getStraightMatter() + js, "text/html", "UTF-8", null);
            }
        });
    }

    public static void startactivity(Context mContext, String id){
        Intent mIntent = new Intent(mContext,QuestionDetailActivity.class);
        mIntent.putExtra("id",id);
        mContext.startActivity(mIntent);

    }
}
